
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Leon
 */
class Menu extends GameCanvas {

    private String[] options = {"New Game", "High Score", "Exit"};
    private Point locationMenu = new Point(30, 40);
    private int itemWidth;
    private int item_space = 30;
    private Image logo;
    private Image itemSelect;
    private Image itemUnselect;
    private Graphics g;
    private int itemHeight;
    private int color = 0xFFFFFF;
    private Font font;
    private MainMidlet mainMidlet;

    Game GAMELV1;
    Game GAMELV2;
    int CurrentLV;
    Command cmdExit = new Command("Exit", Command.EXIT, 0);
    Command nextState = new Command("Qua màn", Command.OK, 0);
    Command startGame = new Command("Bắt đầu", Command.OK, 0);
    Command startAgain = new Command("Chơi lại", Command.OK, 0);

    public Menu(MainMidlet mainM) {
        super(false);
        this.mainMidlet = mainM;

        setFullScreenMode(true);
        try {
            this.g = this.getGraphics();
            logo = Image.createImage("/resource/logo.PNG");
            itemSelect = Image.createImage("/resource/btnPressed.png");
            itemUnselect = Image.createImage("/resource/btnUnPressed.png");
            this.itemHeight = itemSelect.getHeight();
            this.itemWidth = itemUnselect.getWidth();
            this.font = Font.getFont(Font.STYLE_ITALIC, Font.FACE_SYSTEM, Font.SIZE_SMALL);
            this.locationMenu = new Point(this.getWidth() / 2 - this.itemWidth / 2, this.locationMenu.getY());
            g.setFont(font);
        } catch (Exception e) {
        }
    }

    public void Draw() {
        g.setColor(this.color);
        g.drawImage(logo, 40, 0, 0);
        for (int i = 0; i < 3; i++) {
            g.drawImage(itemUnselect, locationMenu.getX(), locationMenu.getY() + i * itemHeight + item_space, Graphics.LEFT | Graphics.TOP);
            g.drawString(options[i], locationMenu.getX() + 10, locationMenu.getY() + i * itemHeight + item_space + 7, Graphics.TOP | Graphics.LEFT);
            this.flushGraphics();
        }
    }

    private void clear_Item(int i) {
        g.setColor(this.color);
        g.drawImage(itemUnselect, locationMenu.getX(), locationMenu.getY() + i * itemHeight + item_space, Graphics.LEFT | Graphics.TOP);
        g.setColor(this.color);
        g.drawString(options[i], locationMenu.getX() + 10, locationMenu.getY() + i * itemHeight + item_space + 7, Graphics.TOP | Graphics.LEFT);
        flushGraphics();
    }

    protected void pointerPressed(int x, int y) {
        super.pointerPressed(x, y);

        if (x < locationMenu.getX() + 180 && x > locationMenu.getX() && y < locationMenu.getY() + 55 && y > locationMenu.getY() + 25) {
            clear_Item(1);
            clear_Item(2);
            g.drawImage(itemSelect, locationMenu.getX(), locationMenu.getY() + item_space, Graphics.LEFT | Graphics.TOP);
            g.drawString(options[0], locationMenu.getX() + +10, locationMenu.getY() + 0 * itemHeight + item_space + 7, Graphics.TOP | Graphics.LEFT);
        }

        if (x < locationMenu.getX() + 180 && x > locationMenu.getX() && y < locationMenu.getY() + 85 && y > locationMenu.getY() + 55) {
            clear_Item(0);
            clear_Item(2);
            g.drawImage(itemSelect, locationMenu.getX(), locationMenu.getY() + 1 * itemHeight + item_space, Graphics.LEFT | Graphics.TOP);
            g.drawString(options[1], locationMenu.getX() + 10, locationMenu.getY() + 1 * itemHeight + item_space + 7, Graphics.TOP | Graphics.LEFT);
        }

        if (x < locationMenu.getX() + 180 && x > locationMenu.getX() && y < locationMenu.getY() + 115 && y > locationMenu.getY() + 85) {
            clear_Item(0);
            clear_Item(1);
            g.drawImage(itemSelect, locationMenu.getX(), locationMenu.getY() + 2 * itemHeight + item_space, Graphics.LEFT | Graphics.TOP);
            g.drawString(options[2], locationMenu.getX() + 10, locationMenu.getY() + 2 * itemHeight + item_space + 7, Graphics.TOP | Graphics.LEFT);
        }
        flushGraphics();
    }

    public void switchDisplay(Alert alert, Displayable newDisplayable) {

        Display display = getDisplay();

        if (alert == null) {

            display.setCurrent(newDisplayable);

        } else {
            display.setCurrent(alert, newDisplayable);
        }
    }

    public Display getDisplay() {
        return Display.getDisplay(this.mainMidlet);
    }
}
